package org.maven.dependencybrowser.gui;
/**********************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:

**********************************************************************/
import org.maven.dependencybrowser.model.Artifact;

import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.util.ArrayList;
import java.util.List;

public class DependencyTreeModel implements TreeModel
{
    private Artifact root;
    private List listeners;

    public DependencyTreeModel(Artifact root)
    {
        this.root = root;
        listeners = new ArrayList();
    }

    public Object getRoot()
    {
        return root;
    }

    public int getChildCount(Object parent)
    {
        Artifact parentArtifact = (Artifact) parent;
        if (parentArtifact.isLeaf())
        {
            return 0;
        }
        return parentArtifact.getDependencies().size();
    }

    public boolean isLeaf(Object node)
    {
        Artifact nodeArtifact = (Artifact) node;
        return nodeArtifact.isLeaf();
    }

    public void addTreeModelListener(TreeModelListener l)
    {
        listeners.add(l);
    }

    public void removeTreeModelListener(TreeModelListener l)
    {
        listeners.remove(l);
    }

    public Object getChild(Object parent, int index)
    {
        Artifact parentArtifact = (Artifact) parent;
        return parentArtifact.getDependencies().get(index);
    }

    public int getIndexOfChild(Object parent, Object child)
    {
        Artifact parentArtifact = (Artifact) parent;
        return parentArtifact.getDependencies().indexOf(child);
    }

    public void valueForPathChanged(TreePath path, Object newValue)
    {
        throw new UnsupportedOperationException();
    }

}
